# from scrapy import Spider, Request
#
# class sdySpider(Spider):
#     name = 'sdy'
#     headers = {
#         'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
#     }
#
#     def start_requests(self):
#         for i in range(1,6):
#             if i == 1:
#                 yield Request(url='http://www.80s3gp.com/drama/index.html'.format(i),
#                               headers=self.headers, callback=self.parse_menu)
#             else:
#                 yield Request(url='http://www.80s3gp.com/drama/index_{}.html'.format(i),
#                               headers=self.headers, callback=self.parse_menu)
#
#     def parse_menu(self, response):
#         movie_title = response.xpath('/html/body/div[4]/div[1]/div[2]/div[1]/ul[1]/li[1]/a/@href').extract()
#         movie = []
#         movie['movie_name'] = response.xpath(u'/html/body/div[4]/div[1]/div[2]/div[1]/ul[1]/li[1]/a/img/@title').extract()[0]
#         movie['movie_img_url'] = 'http://www.80s3gp.com' + response.xpath(u'/html/body/div[4]/div[1]/div[2]/div[1]/ul[1]/li[1]/a/img/@src').extract()
#         for one_movie in movie_title:
#             yield Request(url='/html/body/div[4]/div[1]/div[2]/div[1]/ul[1]/li[1]/a')

from scrapy import  Spider,Request
from movie2 import items

class dy2spider(Spider):
    name='movie80s'

    headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
    }



    def start_requests(self):

        yield Request(url='http://www.80s3gp.com/drama/index.html',headers=self.headers,callback=self.parse_one)
        for i in range(2,6):
            yield Request(url='http://www.80s3gp.com/drama/index_{}.html'.format(i),headers=self.headers,callback=self.parse_one)


    def parse_one(self, response):
        movies=response.xpath('/html/body/div[4]/div[1]/div[2]/div[1]/ul/li[2]/a/@href').extract()
        for i in movies:
            yield Request(url='http://www.80s3gp.com{}'.format(i),headers=self.headers,callback=self.parse_two)


    def parse_two(self, response):

        meta_item = {}
        meta_item['movie_img_url']='http://www.80s3gp.com'+response.xpath('//*[@id="mainzy"]/div[1]/div[3]/div[1]/img/@src').extract()[0]
        meta_item['movie_name']=response.xpath('//*[@id="mainzy"]/div[1]/div[3]/div[2]/div/h1/text()').extract()[0]
        meta_item['movie_info']=response.xpath('//*[@id="mainzy"]/div[1]/div[3]/div[4]/div[2]/text()').extract()[0]
        next_url=response.xpath('//*[@id="mainzy"]/div[1]/div[3]/div[6]/div[1]/ul/li[2]/a/@href').extract()[0]
        yield Request(url='http://www.80s3gp.com{}'.format(next_url),meta={'meta_item':meta_item},headers=self.headers,callback=
                      self.parse_three)


    def parse_three(self, response):

        meta_item=response.meta['meta_item']
        movie_download_url=response.xpath('//*[@id="101939"]/dl/dd/ul/li[2]/a/@href').extract()[0]

        movie=items.MovieItemTwo()
        movie['movie_img_url']=meta_item['movie_img_url']
        movie['movie_name']=meta_item['movie_name']
        movie['movie_info']=meta_item['movie_info']
        movie['movie_download_url']=movie_download_url
        yield movie

